<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	
	private $viewdata = array();
	
	function __construct()
	{
		parent::__construct();
	
		$this->load->library('session');
		
			
	}
	
####################################################################################################################	
//login function	
function index()
	{
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username','Username','required|xss_clean');
		$this->form_validation->set_rules('password','Passsword','required|xss_clean');
		
		if($this->form_validation->run() != false){	
			
			$this->load->model('user_model');
			$valideuser = $this->user_model->veryfy_user($this->input->post('username'),$this->input->post('password'));
				
			if($valideuser != false)
			{
				//valid user. need to updates system logger
				$row = $valideuser->row_array();
				$username = $row['username'];
				$userid =  $row['user_id'];
				$accesslevel = $row['accessLevel'];
				$this->load->library('footprintlog');
				$login_check = $this->footprintlog->footPrintLogger($userid,'login', null);
				
				//if system logger is sucessfully updated, continue
				if($login_check != false)
				{
					$this->session->set_userdata('username', $username);
					$this->session->set_userdata('user_id', $userid);
					$this->session->set_userdata('accessLevel', $accesslevel);
					$this->session->set_userdata('access_id', $login_check);
					echo "Welcome Message";
								
				}
				
				else
				{
					//system logger update failed.
					echo "Database Error";
				}	

			}
			
			else
			{
				//validation failed. invalid user
				echo "Incorrect username or password";
			}
	
		}
		
		else 
		{
			//form validation failed
			echo "Username and password fields are required";

		}

	}

############################################################################################################
//user area include hear
	
	function welcome()
	{
		if($this->session->userdata('user_id') == false)
		{
			redirect(base_url());
		}
		else
		{
		
		$this->load->view('dataentry_terminal');
		
		}
	}
############################################################################################################
//signup function
	function signup()
	{
		
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('fname','First Name','required|xss_clean');
		$this->form_validation->set_rules('lname','Last Name','required|xss_clean');
		$this->form_validation->set_rules('uemail','Email Address','required|valid_emails|xss_clean');
		$this->form_validation->set_rules('username_su','username','required|xss_clean|callback_username_check');
		$this->form_validation->set_rules('password_su', 'Password', 'required|xss_clean|min_length[8]');
		$this->form_validation->set_rules('password_suc', 'Confirm password', 'required|xss_clean|callback_password_check');
		
		
		
		if($this->form_validation->run() != false)
		{
				
			$this->load->model('user_model');
			
			$params = array('iterationCount'=>'8', 'portableHashes'=>'false');
			$this->load->library('PasswordHash', $params);
			
		
			$valideuser = $this
			->user_model
			->user_register(
					$this->input->post('fname'),
					$this->input->post('lname'),
					$this->input->post('uemail'),
					$this->input->post('username_su'),
					$this->passwordhash->HashPassword($this->input->post('password_su')),
					$this->input->post('accessLevel')
			);
				
			$message = "User successfully registered";
			$bg_color = "#FFEBE8";
				
		}
		else
		{
			$message = form_error('fname');
			$message .= form_error('lname');
			$message .= form_error('uemail');
			$message .= form_error('username_su');
			$message .= form_error('password_su');
			$message .= form_error('password_suc');
			$bg_color = "#FFEBE8";
		}
		
		
		
		$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';
		echo $output;
		
		
	}
###############################################################################################################
//logout function

	function logout()
	{
	
		$this->session->sess_destroy();
	
		redirect(base_url());
	}
	
#########################################################################################################
//password check
	function password_check($str)
	{
		
		$password = $this->input->post('password_su');
		$cpassword = $this->input->post('password_suc');
		
		
		if($password == $cpassword)
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('password_check', 'Confirm password not match');
			return FALSE;
		}
	}
################################################################################################################
//check username
	function username_check($str)
	{
	
		$this->load->model('user_model');
			
		$valideuser1 = $this
		->user_model
		->is_registered_user($str);
	
		if($valideuser1 != false)
		{
			$this->form_validation->set_message('username_check', 'Username username already exists');
			return FALSE;
		}
		else
		{
	
			return TRUE;
	
		}
	
	
	}
}
